package com.yunxi.module.system.controller.admin.permission;

import com.yunxi.framework.apilog.core.annotation.ApiAccessLog;
import com.yunxi.framework.apilog.core.enums.OperateTypeEnum;
import com.yunxi.framework.common.enums.CommonStatusEnum;
import com.yunxi.framework.common.pojo.CommonResult;
import com.yunxi.framework.common.pojo.PageParam;
import com.yunxi.framework.common.pojo.PageResult;
import com.yunxi.framework.common.util.object.BeanUtils;
import com.yunxi.framework.excel.util.ExcelUtils;
import com.yunxi.module.system.model.permission.RoleDO;
import com.yunxi.module.system.service.permission.RoleService;
import com.yunxi.module.system.vo.admin.permission.role.RolePageReqVO;
import com.yunxi.module.system.vo.admin.permission.role.RoleRespVO;
import com.yunxi.module.system.vo.admin.permission.role.RoleSaveReqVO;
import com.yunxi.module.system.vo.admin.permission.role.RoleUpdateStatusReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 管理后台 - 角色
 * </p>
 *
 * @author lidy
 * @since 2024-05-28
 */
@Tag(name = "管理后台 - 角色")
@RestController
@RequestMapping("/system/role")
@Validated
public class RoleController {

    @Resource
    private RoleService roleService;

    @PutMapping("/updateStatus")
    @Operation(summary = "修改角色状态")
    @PreAuthorize("@ss.hasPermission('system:role:update')")
    public CommonResult<Boolean> updateRoleStatus(@Valid @RequestBody RoleUpdateStatusReqVO reqVO) {
        roleService.updateRoleStatus(reqVO.getId(), reqVO.getStatus());
        return CommonResult.success(true);
    }

    @GetMapping("/exportRole")
    @Operation(description = "导出角色 Excel")
    @PreAuthorize("@ss.hasPermission('system:role:export')")
    @ApiAccessLog(operateType = OperateTypeEnum.EXPORT)
    public void exportRole(HttpServletResponse response, @Validated RolePageReqVO reqVO) throws IOException {
        // 设置不分页
        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 查询数据
        List<RoleDO> roleList = roleService.getRolePage(reqVO).getList();
        // 输出
        ExcelUtils.write(response, "角色数据.xlsx", "角色数据",
                RoleRespVO.class, BeanUtils.toBean(roleList, RoleRespVO.class));
    }

    @DeleteMapping("/delete")
    @Operation(description = "删除角色")
    @PreAuthorize("@ss.hasPermission('system:role:delete')")
    @Parameter(name = "roleId", description = "角色编号", required = true, example = "1024")
    public CommonResult<Boolean> deleteRole(@RequestParam("roleId") Long roleId) {
        roleService.deleteRole(roleId);
        return CommonResult.success(true);
    }

    @PostMapping("/create")
    @Operation(summary = "创建角色")
    @PreAuthorize("@ss.hasPermission('system:role:create')")
    public CommonResult<Long> createRole(@Valid @RequestBody RoleSaveReqVO createReqVO) {
        Long roleId = roleService.createRole(createReqVO, null);
        return CommonResult.success(roleId);
    }

    @PutMapping("/update")
    @Operation(summary = "修改角色")
    @PreAuthorize("@ss.hasPermission('system:role:update')")
    public CommonResult<Boolean> updateRole(@Valid @RequestBody RoleSaveReqVO updateReqVO) {
        roleService.updateRole(updateReqVO);
        return CommonResult.success(true);
    }

    @GetMapping("/get")
    @Operation(description = "获得角色信息")
    @PreAuthorize("@ss.hasPermission('system:role:query')")
    public CommonResult<RoleRespVO> getRole(@RequestParam("roleId") Long roleId) {
        RoleDO roleDO = roleService.getRole(roleId);
        return CommonResult.success(BeanUtils.toBean(roleDO, RoleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(description = "获得角色分页列表")
    @PreAuthorize("@ss.hasPermission('system:role:query')")
    public CommonResult<PageResult<RoleRespVO>> getRolePage(RolePageReqVO reqVO) {
        PageResult<RoleDO> pageResult = roleService.getRolePage(reqVO);
        return CommonResult.success(BeanUtils.toBean(pageResult, RoleRespVO.class));
    }

    @GetMapping({"listAllSimple", "simpleList"})
    @Operation(summary = "获得角色精简信息", description = "只获取开启的角色，主要用于前端下拉框")
    public CommonResult<List<RoleRespVO>> getSimpleList() {
        List<RoleDO> roleDOS = roleService.getRoleListByStatus(Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        // 排序
        roleDOS.sort(Comparator.comparing(RoleDO::getSort));
        return CommonResult.success(BeanUtils.toBean(roleDOS, RoleRespVO.class));
    }

}
